home *** CD-ROM | disk | FTP | other *** search
- // -------------------------------------------------------------------------------------
- // PortComm.h
- // serial port communication handler
- // Author: Steve Herrick, Martin D. Flynn, NeXT Computer, Inc.
- // -------------------------------------------------------------------------------------
- // THIS CODE FRAGMENT IS FOR DEMONSTRATION PURPOSES ONLY.
- // Permission is granted to freely redistribute this source code, and to use fragments
- // of this code in your own applications if you find them to be useful. This class,
- // along with the source code, come with no warranty of any kind, and the user assumes
- // all responsibility for its use.
- // -------------------------------------------------------------------------------------
-
- // -------------------------------------------------------------------------------------
- #import <mach/cthreads.h>
- #import <sys/ttydev.h>
- #import <sys/ioctl.h>
- #import <objc/Object.h>
-
- // -------------------------------------------------------------------------------------
-
- /* port numbers */
- #define portNumA 0
- #define portA "/dev/ttya"
-
- #define portNumB 1
- #define portB "/dev/ttyb"
-
- /* parity */
- #define parityANY ANYP
- #define parityODD ODDP
- #define parityEVEN EVENP
- #define parityNONE 0
- #define parityRAW RAW // 8 bit, no i/o processing
- #define parityRAW_eol (0x8000 | parityRAW) // RAW with EOL checking
- #define parityUNKNOWN parityNONE
-
- /* errors */
- #define noERROR 0
- #define errorOPEN 1
- #define errorTIOCGETP 2
- #define errorTIOCSETP 3
- #define errorBADPORTNUM 4
- #define errorTIOCEXCL 5
- #define errorTIOCSETD 6
- #define errorTIOCCBRK 7
- #define errorTIOCSDTR 8
- #define errorTIOCLSET 9
- #define errorTIOCNOTTY 10
- #define errorTIOCSLTC 11
-
- // -------------------------------------------------------------------------------------
- @interface PortComm : Object
- {
-
- id dataHandler; // data record handler
-
- char *rcdBuff; // record buffer
- int maxBytes; // default max number of bytes to read
- u_char charMask; // character mask
- BOOL checkEol; // check EOL even if parity if RAW
-
- mutex_t forkMutex; // fork/kill mutex
- mutex_t writeMutex; // port write mutex
-
- cthread_t portThread; // port listener thread number
- BOOL isListening; // port listener running flag
-
- char portNumber; // port number (0 == A, 1 == B)
- char *portDevice; // port device file name
- char portBaud; // port baud rate
- short portParity; // port parity
-
- int portFd; // port file descriptor
- int portError; // chad error
-
- }
-
- // -------------------------------------------------------------------------------------
- // New instance
- + newPort:(int)portNum baud:(u_char)baudRate parity:(u_short)parity charMask:(u_char)mask;
- + newPort:(int)portNum baud:(u_char)baudRate parity:(u_short)parity;
- // These methods create a new instance of a serial port handling object. portNum is
- // either 0, or 1, (for serial ports A & B, respectively). baudRate is the port
- // configuration flag to specify the port Baud rate (see 'baudRateConstant:'). parity is
- // is any of the following constant values (parityANY, parityODD, parityEVEN, parityNONE,
- // parityRAW, or parityRAW_eol). charMask is a mask with which each character read from
- // the port is AND'ed with. Typical masks are 0xFF, or 0x7F (used to force the ascii
- // parity bit to be stripped).
- //
- // -------------------------------------------------------------------------------------
- // Close/free serial port
- - free;
- // Close and free the port handling object. WARNING: If "forkPortListener" has been
- // issued, this object should never bee freed!
- //
- // -------------------------------------------------------------------------------------
- // Return actual device name used for specified port number
- + (const char *)getPortDevice:(int)portNum;
- // This method returns a constant specifying the device name corresponding to the specified
- // port number.
- //
- // -------------------------------------------------------------------------------------
- // Return the Baud rate flag constant for the specified baud rate number
- + (u_char)baudRateConstant:(int)baudRate;
- // BaudRate may be any of the following: 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
- // 1800, 2400, 4800, 9600, 19200, or 38400.
- //
- // -------------------------------------------------------------------------------------
- // Write to port
- - (int)writeToPort:(char *)buff length:(int)buffLen;
- - (int)writeToPort:(char *)buff;
- // These methods write data to the serial port. 'writeToPort:' writes a null terminated
- // string. and 'writeToPort:length:' writes buffLen characters to the port.
- //
- // -------------------------------------------------------------------------------------
- // Read from port
- - (int)read:(char *)buff maxLen:(int)maxLen timeout:(float)timeout;
- // This methods ateempt to read maxLen number of bytes from the serial port. if no
- // characters are present after timeout seconds, -1 is returned, otherwise the actual
- // number of characters read is returned.
- //
- // -------------------------------------------------------------------------------------
- // Continue reading characters from port
- - readLoop:(float)timeout;
- // This method will continue to wait for characters to be ready on the serial port.
- // When characters are available, they will be read and sent to the data handler object
- // via a method call to 'dataRecord:len:fromPort:'. Then this method will resume waiting
- // for characters to read on the serial port. This method should only be used within
- // a backgroup thread (see 'forkPortListener').
- //
- // -------------------------------------------------------------------------------------
- // Wait for available characters on serial port
- - (int)readTimeout:(float)timeout;
- // This method will wait for characters to become available on the serial port and return
- // a value greater than zero if characters are available, 0 if the timeout has elapsed,
- // and a value less than zero if an error has occurred.
- //
- // -------------------------------------------------------------------------------------
- // Start/stop serial port data collector
- - forkPortListener;
- // This method starts the thread controlled serial port data reader. 'forkPortListener'
- // forks a thread and issues method call 'readLoop:0.0'. Once this method has been
- // issued, this object should persist for the remainder of the life of the application.
- // (ie. This object should never be 'free'd). The data read from the port will be
- // sent to the data handler method 'dataRecord:len:fromPort:' (see readLoop:). It is
- // important to note that since dataRecord:len:fromPort: will be called from a secondary
- // thread the data handler object should not try to perform any drawing in a window view.
- //
- // -------------------------------------------------------------------------------------
- // Return error flags
- + (int)openError;
- - (int)portError;
- // These methods may be used to help with debugging errors which may occur while
- // openning a serial port, or while writing, or reading, a serial port. Possible errors
- // are: noERROR, errorOPEN, errorTIOCGETP, errorTIOCSETP, errorBADPORTNUM, errorTIOCEXCL,
- // errorTIOCSETD, errorTIOCCBRK, errorTIOCSDTR, errorTIOCLSET, errorTIOCNOTTY,
- // or errorTIOCSLTC.
- //
- // -------------------------------------------------------------------------------------
- // Set data handler for "readLoop:"
- - setDataHandler:handler;
- - (BOOL)dataRecord:(char*)buff len:(int)len fromPort:(int)portNum;
- // 'setDataHandler:' specifies an object that will be messaged within 'readLoop:' when
- // characters have been read from the serial port. The method sent to the data handler
- // is 'dataRecord:len:formPort:'. buff will contain the characters read from the port,
- // len will specify the number of characters read, and portNum will be the serail port
- // from which the characters were read.
- //
- // -------------------------------------------------------------------------------------
- // Set maximum buffer read size
- - setMaxReadSize:(int)size;
- // This method sets the maximum number of bytes read from the serial port during a
- // single read. The default is 1024.
- //
- // -------------------------------------------------------------------------------------
-
- @end
-